package com.goldsign.pay.provider.sample.configuration;

import com.alibaba.druid.pool.DruidDataSource;  
import com.alibaba.druid.support.http.StatViewServlet;  
import com.alibaba.druid.support.http.WebStatFilter;  
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;  
import org.springframework.boot.web.servlet.ServletRegistrationBean;  
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;  
import org.springframework.boot.web.servlet.FilterRegistrationBean;  
import org.springframework.boot.web.servlet.ServletRegistrationBean;  
import org.springframework.context.EnvironmentAware;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.core.env.Environment;  
import org.springframework.transaction.annotation.EnableTransactionManagement;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

//@Configuration
public class DruidDataSourceConf implements EnvironmentAware {

//	private RelaxedPropertyResolver propertyResolver;  
	private Environment environment;  
	
    public void setEnvironment(Environment env) {  
//        this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");  
        this.environment = env;
    }  
  
//    @Bean
//    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {  
    	String prefix = "spring.datasource.";
    	System.out.println(environment.getProperty( prefix + "url"));
        DruidDataSource datasource = new DruidDataSource();  
        datasource.setUrl(environment.getProperty( prefix + "url"));  
        datasource.setDriverClassName(environment.getProperty( prefix + "driver-class-name"));  
        datasource.setUsername(environment.getProperty( prefix + "username"));  
        datasource.setPassword(environment.getProperty( prefix + "password"));  
        datasource.setInitialSize(Integer.valueOf(environment.getProperty( prefix + "initialSize")));  
        datasource.setMinIdle(Integer.valueOf(environment.getProperty(prefix +  "minIdle")));  
        datasource.setMaxWait(Long.valueOf(environment.getProperty( prefix + "maxWait")));  
        datasource.setMaxActive(Integer.valueOf(environment.getProperty( prefix + "maxActive")));  
        datasource.setMinEvictableIdleTimeMillis(  
                Long.valueOf(environment.getProperty( prefix + "minEvictableIdleTimeMillis")));  
        try {  
            datasource.setFilters("stat,wall");  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return datasource;  
    }  
}
